/******************************************************************************* 
  Copyright (C), 1988-2020, zeno Tech. Co., Ltd.
  Description:   VIDEO debug模块  
  Function List:     
  History:
    <huangzhendi -- 2014-09-26>  创建
*******************************************************************************/ 

/*=============================================================================
                                 Includes                                  
 =============================================================================*/
#include "videoPriv.h"
#include "videoIsp.h"
#include "VideoDec.h"

/*=============================================================================
                               Macro Definition
 =============================================================================*/
#define VIDEO_CALC_ALIGN(size, align)    (size + (align - size%align)%align)


/*=============================================================================
                        Local Structures and Typedef 
 =============================================================================*/


/*=============================================================================
                             Extern Variables                                  
=============================================================================*/

/*=============================================================================
                             Local Variables                                  
 =============================================================================*/ 

/*=============================================================================
                             Function Definition                                  
 =============================================================================*/
#ifndef __HuaweiLite__
/*******************************************************************************
  Function:     ENC_RecordUsage
  Description:  显示用法
  Input:        无
  Output:       无
  Return:       无
  Others:       无
*******************************************************************************/
static void VIDEO_RecordUsage(void)
{
    LOG_Print("Usage:\n");
    LOG_Print("echo %s streamId frameNum isPackData recordPath "\
                "recordName > progress name \n", "record");
}


/*******************************************************************************
  Function:     VIDEO_RecordFun
  Description:  录像命令执行函数
  Input:        - count: 参数个数
                - pInput: 参数字符串数组
  Output:       无
  Return:       0: 操作成功
                -1: 操作失败
  Others:       无
*******************************************************************************/
static int VIDEO_RecordFun(int count, char *pInput[])
{ 
	unsigned int streamId, frameNum, isPackData;
    ENC_ctl_t *pEncCtl;

    if (count < 3 || count > 5)
    {
        VIDEO_RecordUsage();
        return -1;
    }

    streamId = (unsigned int)atoi(pInput[0]);
    frameNum = (unsigned int)atoi(pInput[1]);
    isPackData = (unsigned int)atoi(pInput[2]);

    if (streamId >= ENC_STREAM_PER_CHN)
    {
        LOG_PrintError("streamId is invalid!\n");
        return -1;
    }

    pEncCtl = videoCtl_g.pEncCtls[streamId];
    if (!pEncCtl)
    {
        LOG_PrintError("pEncCtl is NULL!\n");
        return -1;
    }

    pEncCtl->recordData.recordFlag = 1;
    pEncCtl->recordData.recordFd = -1;
    pEncCtl->recordData.streamId = streamId;
    pEncCtl->recordData.frameNum = frameNum;
    pEncCtl->recordData.isPackData = isPackData;

    if (3 == count)
    {
        strcpy(pEncCtl->recordData.recordPath, "./");
        sprintf(pEncCtl->recordData.recordName, "encoded_data_ch%d_%s", 
                    streamId, (isPackData == 1)?"dav":"raw");
    }
    else if (4 == count)
    {
        strcpy(pEncCtl->recordData.recordPath, pInput[3]);
        sprintf(pEncCtl->recordData.recordName, "encoded_data_ch%d_%s", 
                    streamId, (isPackData == 1)?"dav":"raw");
    }
    else if (5 == count)
    {
        strcpy(pEncCtl->recordData.recordPath, pInput[3]);
        strcpy(pEncCtl->recordData.recordName, pInput[4]);
    }

    return 0;
}
 

/*******************************************************************************
  Function:     VIDEO_ResetSensor
  Description:  手动sensor复位
  Input:        - count: 参数个数
                - pInput: 参数字符串数组
  Output:       无
  Return:       0: 操作成功
                -1: 操作失败
  Others:       无
*******************************************************************************/
static int VIDEO_ResetSensor(int count, char *pInput[])
{
    if (videoCtl_g.pVideoIsp)
    {
        return videoCtl_g.pVideoIsp->pSnsReboot(videoCtl_g.pVideoIsp);
    }
    
    return 0;
}


/*******************************************************************************
  Function:     VIDEO_CatchVpssFrame
  Description:  抓取vpss通道一帧数据
  Input:        - count: 参数个数
                - pInput: 参数字符串数组
  Output:       无
  Return:       0: 操作成功
                -1: 操作失败
  Others:       无
*******************************************************************************/
static int VIDEO_CatchVpssFrame(int count, char *pInput[])
{
    return 0;
}

static char testBuf[1024*1024*2];
static char rgbBuf[1024*1024*4];

/*******************************************************************************
  Function:     VIDEO_Jpegtorgb
  Description:  抓取vpss通道一帧数据
  Input:        - count: 参数个数
                - pInput: 参数字符串数组
  Output:       无
  Return:       0: 操作成功
                -1: 操作失败
  Others:       无
*******************************************************************************/
static int VIDEO_Jpegtorgb(int count, char *pInput[])
{
    int ret;
    int jpegFd, rgbFd;
    VideoDecTransParam jpegToRgb;
    int filelen;

    jpegFd = open("./test.jpeg", O_RDONLY, 0777);
    if (jpegFd < 0)
    {
        LOG_Print("Fail to open test.jpeg\n");
        return -1;
    }
    filelen= lseek(jpegFd, 0, SEEK_END);  
    lseek(jpegFd, 0, SEEK_SET);  
    read(jpegFd, testBuf, filelen);
    close(jpegFd);

	jpegToRgb.inType = videoDecJpeg;
	jpegToRgb.jpegData = testBuf;
	jpegToRgb.jpegLen = filelen;
	jpegToRgb.cropX = 0;
	jpegToRgb.cropY = 0;
	jpegToRgb.cropWidth = 0;
	jpegToRgb.cropHeight = 0;
	jpegToRgb.dstWidth = 800;
	jpegToRgb.dstHeight = 800;
	jpegToRgb.outType = videoDecBgr;
	jpegToRgb.rbgData = rgbBuf;
	jpegToRgb.rgbLen = sizeof(rgbBuf);
    //ret = videoDecJpegToRgb(&jpegToRgb);
    if (ret)
    {
        LOG_Print("Fail to jpeg to rgb\n");
        return -1;
    }

    rgbFd = open("./test.rgb", O_RDWR | O_CREAT | O_TRUNC, 0777);
    if (jpegFd < 0)
    {
        LOG_Print("Fail to open test.yuv\n");
        return -1;
    }
    write(rgbFd, rgbBuf, jpegToRgb.dstWidth*jpegToRgb.dstHeight*3);
    fsync(rgbFd);
    close(rgbFd);

    return 0;
}


/*******************************************************************************
  Function:     VIDEO_RegRecordCmd
  Description:  注册调试命令
  Input:        无
  Output:       无
  Return:       0: 操作成功
                -1: 操作失败
  Others:       无
*******************************************************************************/
int VIDEO_RegCmd(void)
{
    SYS_cmdParams_t cmdParams;

    memset(&cmdParams, 0, sizeof(SYS_cmdParams_t));
    strcpy(cmdParams.cmdName, "record");
    cmdParams.func = VIDEO_RecordFun;
    SYS_DebugRegCmd(&cmdParams);
    
    memset(&cmdParams, 0, sizeof(SYS_cmdParams_t));
    strcpy(cmdParams.cmdName, "resetsensor");
    cmdParams.func = VIDEO_ResetSensor;
    SYS_DebugRegCmd(&cmdParams);

    memset(&cmdParams, 0, sizeof(SYS_cmdParams_t));
    strcpy(cmdParams.cmdName, "vpssframe");
    cmdParams.func = VIDEO_CatchVpssFrame;
    SYS_DebugRegCmd(&cmdParams);

    memset(&cmdParams, 0, sizeof(SYS_cmdParams_t));
    strcpy(cmdParams.cmdName, "jpegtorgb");
    cmdParams.func = VIDEO_Jpegtorgb;
    SYS_DebugRegCmd(&cmdParams);

    return 0;
}
#endif


